<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/14
 * Time: 15:05
 */

namespace App\Http\Controllers\Web;


use App\Service\OrderService;
use App\Http\Controllers\Api\WechatController;

class OrderController extends BaseController
{
    public function index()
    {
        $params = $this->request->post();
        $page = $this->request->post('page',1);
        $limit = $this->request->post('limit',15);
        
        return OrderService::getList($params,'room_id','ASC',$page,$limit);

    }

    public function getOrderById()
    {
        $orderId = $this->request->post('id');
        if(!$orderId){
            return Response()->json(['code'=>200,'msg'=>'参数缺失']);
        }
        return OrderService::getOrderById($orderId);
    }


    public function edit()
    {
        $params = $this->request->post();
        if(!isset($params['id']) || empty(intval($params['id']))){
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }

        return OrderService::edit($params);


    }

    public function delivery()
    {
        $params = $this->request->post();
        if(isset($params['building_no']) || isset($params['floor_no'])){
            if(!intval($params['building_no']) || !intval($params['floor_no'])){
                return Response()->json(['code'=>201,'msg'=>'参数缺失']);
            }
        }elseif(isset($params['id'])){
            if(!intval($params['id'])){
                return Response()->json(['code'=>201,'msg'=>'参数缺失']);
            }
        }else{
            return Response()->json(['code'=>201,'msg'=>'参数缺失']);
        }

        return OrderService::delivery($params);
    }

    public function destroy()
    {
        $ids = $this->request->post('ids');
        $ids = is_array($ids) ? $ids : [$ids];
        if(!$ids){
            return Response()->json(['code'=>201,'msg'=>'请选择要删除的订单']);
        }
        return OrderService::destroy($ids);

    }
    
    public function orderRefund()
    {
        try{
            $params = $this->request->post();
            if(!isset($params['order_id']) || !intval($params['order_id'])){
                throw new \Exception('请选择要退款的订单');
            }
            $params['reason'] = $this->request->post('reason','');
            $result = (new WechatController())->orderRefund($params['order_id'],$params['reason']);
            
        }catch(\Exception $e){
            return Response()->json(['code'=>201,'msg'=>$e->getMessage()]);
        }

        return Response()->json(['code'=>200,'msg'=>'已退款，退款金额会有延迟']);
    }
    

}
